package com.imc.platform.base.lesson.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.imc.api.order.pojo.Lesson;
import com.imc.api.order.pojo.LessonAtt;
import com.imc.api.order.pojo.LessonSchedule;
import com.imc.core.utils.MysqlUtil;
import com.imc.platform.base.category.pojo.Category;
import com.imc.platform.base.lesson.dao.mapper.BackLessonMapper;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import java.security.Principal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Created by tanweiping on 2018/10/10.
 */
@Slf4j
@Service
@Transactional
public class LessonService {

    @Autowired
    BackLessonMapper backLessonMapper;

    @Autowired
    MysqlUtil mysqlUtil;

    public PageInfo<Map<String,Object>> queryList(String roleId,String orgId,String parm, Pageable pageable){
        Map<String,Object>parms=new HashMap<>();
        parms.put("roleId",roleId);
        if("lesson".equals(parm)){
            parms.put("orgId",orgId);
            parms.put("lessonType","1");
        }else if ("voide".equals(parm)){
            parms.put("lessonType","2");
            parms.put("orgId","");
        }else if("meal".equals(parm)){
            parms.put("lessonType","3");
            parms.put("orgId",orgId);
        }
        PageHelper.startPage(pageable.getPageNumber(),pageable.getPageSize());
        val data = backLessonMapper.queryLessonM(parms);
        for(Map map : data){
            map.put("pageNumber",pageable.getPageNumber());
            map.put("pageSize",pageable.getPageSize());
        }
        PageInfo info= new PageInfo<>(data);
        return info;
    }

    public List<Category> queryCatAll(String catType){
        return backLessonMapper.queryCatAll(catType);
    }

    public void addLesson(Lesson lesson){
        backLessonMapper.addLesson(lesson);
//        backLessonMapper.addLessonCat(lesson);
    }

    public void addSchedule(LessonSchedule lessonSchedule){
        backLessonMapper.addSchedule(lessonSchedule);
    }


    public void deleteLessonById(String id){
//        Lesson lesson =backLessonRepository.findOne(id);
//        if (lesson.getState().equals("1")){
//            lesson.setState("0");
//        }else {
//            lesson.setState("1");
//        }
//        backLessonRepository.save(lesson);
        backLessonMapper.delLessonById(id);
    }

    public void updateLesson(Lesson lesson){
        backLessonMapper.updateLesson(lesson);
    }

    public void updateSchedule(LessonSchedule lessonSchedule){
        backLessonMapper.updateSchedule(lessonSchedule);
    }

    public PageInfo<Map<String,Object>> queryTeacherList(Pageable pageable){
        PageHelper.startPage(pageable.getPageNumber(),pageable.getPageSize());
        val data = backLessonMapper.queryTeacherList();
        PageInfo info= new PageInfo<>(data);
        return info;
    }

    @Transactional(isolation = Isolation.REPEATABLE_READ)
    public PageInfo<Map<String,Object>> queryScheduleList(String lessonId, Pageable pageable){
        PageHelper.startPage(pageable.getPageNumber(),pageable.getPageSize());
        val data = backLessonMapper.queryScheduleList(lessonId);
        PageInfo info= new PageInfo<>(data);
        return info;
    }

    public PageInfo<Map<String,Object>> queryListBySelect(String search,Pageable pageable){
        PageHelper.startPage(pageable.getPageNumber(),pageable.getPageSize());
        val data = backLessonMapper.queryListBySelect(search);
        PageInfo info= new PageInfo<>(data);
        return info;
    }

    public Lesson queryLessonDetByLessonId(String lessonId){
        Lesson lesson=backLessonMapper.queryLessonDetByLessonId(lessonId);
        lesson.setLessonAttList(backLessonMapper.queryLessonAttList(lessonId));
        return lesson;
    }

    public void delScheduleById(String id){
        backLessonMapper.delScheduleById(id);
    }

    public LessonSchedule queryScheduleDetByScheduleId(String scheduleId){
        return backLessonMapper.queryScheduleDetByScheduleId(scheduleId);
    }

    public void addLessonAtt(LessonAtt lessonAtt){
        backLessonMapper.addLessonAtt(lessonAtt);
    }

    public void delLessonAtt(String id,String imgUrl){
        backLessonMapper.delLessonAtt(id,imgUrl);
    }

    @Transactional(isolation = Isolation.REPEATABLE_READ)
    public PageInfo<Map<String,Object>> queryLessonChartList(Map<String,Object>parms, Pageable pageable){
        PageHelper.startPage(pageable.getPageNumber(),pageable.getPageSize());
        val data = backLessonMapper.queryLessonChartList(parms);
        PageInfo info= new PageInfo<>(data);
        return info;
    }
}
